package ufpb.compiladores.lexico;

import java.util.ArrayList;

/**
 *
 * @author SemiNick
 */
public class LexicalTable {

    private ArrayList<Entry> list = new ArrayList<Entry>();
    private int index=-1;
    
    public void addEntry(String token, String descricao, int linha){

        Entry entry = new Entry(token, descricao, linha);
        list.add(entry);
    }
    
    public void addEntry(Entry entry){

        list.add(entry);
    }
    
    public Entry getNext(int offset){

        if (index + offset >= this.list.size()) {
            
            return null;
        }
        
        return list.get(index + offset);
    }
    
    public Entry getCurrent(){

        if (index == this.list.size()) {
            
            return null;
        }
        
        return list.get(index);
    }
    
    public boolean next(){

        if (index == this.list.size()){
            
            return false;
        }

        index++;
        return true;
    }
    
    public boolean hasNext(){

        if ((index+1) < list.size())
            return true;

        return false;
    }
    
    public boolean hasPrevious(){

        if ((index-1) >= 0 )
            return true;

        return false;
    }

    public boolean previous() {

        if (hasPrevious()){
            index--;
            return true;
        }

        return false;
    }

    public Entry getPrevious() {

        if (hasPrevious()){
            return this.getNext(-1);
        }

        return null;
    }

    public void showArrayList(){

        for (int i=0; i<list.size(); i++){

            System.out.println("Token: "+list.get(i).token);
            System.out.println("Description: "+list.get(i).desc);
            System.out.println("Line: "+list.get(i).line+"\n");
        }
    }
    
    public static class Entry {
        
        private final String token;
        private final int line;
        private final String desc;
        
        public Entry(String token, String descricao, int linha) {
            
            if (token ==  null) {
                
                throw new NullPointerException("Token cannot be null");
            } else if (linha <= 0) {
                
//                throw new IllegalArgumentException("Line must be greater than zero");
            }
            
            this.token = token;
            this.line = linha;
            this.desc = descricao;
        }
        
        public final String getToken() {
            
            return this.token;
        }
        
        public final String getDescription() {
            
            return this.desc;
        }
        
        public final int getLine() {
            
            return this.line;
        }
    }
}
